﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucLibsFile : System.Web.UI.UserControl
    {
        cmsLibsFileDO objArt = new cmsLibsFileDO();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdfFile.Value = "";
            }
            if (Request.QueryString["FileID"] != null)
            {
                objArt.FileID = int.Parse(Request.QueryString["FileID"].ToString());
                initForm();
            }
            
        }

        private void initForm()
        {
            objArt = new cmsLibsFileBL().Select(objArt);
            txtTitle.Text = objArt.Title;
            txtDescription.Text = objArt.Description;
            hplImage.NavigateUrl = "~/Libs/" + objArt.FileUrl;
            hplImage.Text = objArt.FileUrl;
            hdfFile.Value = objArt.FileUrl;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            initObject();
            if (objArt.FileID <= 0)
            {

                objArt.UserCreate = int.Parse(Session["UserID"].ToString());
                new cmsLibsFileBL().Insert(objArt);
            }
            else
            {
                new cmsLibsFileBL().Update(objArt);
            }
            Functions.Alert("Cập nhật thành công!", "Default.aspx?Page=ListLibFile");
        }

        private void initObject()
        {
            objArt.Title = txtTitle.Text;
            objArt.Description = txtDescription.Text;
            objArt.CategoryID = 0;
            if (!string.IsNullOrEmpty(fuImage.FileName))
            {
                objArt.FileUrl = UploadFile(fuImage);
                objArt.FileExtension = objArt.FileUrl.Substring(objArt.FileUrl.LastIndexOf("."));
            }
            else
            {
                objArt.FileUrl = hdfFile.Value;
            }
        }

        private string UploadFile(FileUpload fulImage)
        {
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Libs/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=ListLibFile");
        }
    }
}